﻿--[[
   @brief    大界面社交头部消息气泡管理
   @author   rickonzhang
   @version  1.0
   @date     2016-06-14
--]]
-- 有新的群消息
-- 有新的好友消息
-- 有新的上线消息
-- 有新的讨论组消息

social_title_msg_tips_mgr = 
{
    main_mode = 0;
    is_max_social_visible = false;
    is_min_social_visible = false;

    is_click_min_mode_social_tab = false;
    current_active_talk_session_id = "";

    social_unread_msgbox_count = 0;
    online_msg = "";
    friend_msg_count = 0;
    group_msg_count = 0;
    discuss_msg_count = 0;
    live_room_msg_count = 0;
    new_live_room_count = 0;
};

function social_title_msg_tips_mgr.log(log_str)
    base_utility.log("[social_title_msg_tips_mgr] "..log_str);
end

function social_title_msg_tips_mgr.init()
    social_title_msg_tips_mgr.log("init");
    event_center.add_listener(web_register_event.REGISTER_SUCCESS_EVENT,
                    "social_title_msg_tips_mgr.handle_register_event");
end

function social_title_msg_tips_mgr.handle_register_event(event_id, json_str)
    local param_table = base_utility.json_to_table(json_str);
    if nil == param_table then 
        return 
    end
    if param_table.event_name ~= nil and "social_update_msg_event" == param_table.event_name then 
        social_title_msg_tips_mgr.update_tips_msg_info();
    end
end

--消息
function social_title_msg_tips_mgr.recv_notify(event_id, json_str)
    local param = base_utility.json_to_table(json_str);
    if param.number ~= social_title_msg_tips_mgr.social_unread_msgbox_count then
        social_title_msg_tips_mgr.social_unread_msgbox_count = param.number;
        social_title_msg_tips_mgr.update_tips_msg_info({});
    end
    -- body
end

function social_title_msg_tips_mgr.open_message_box(event_id, json_str)
    if social_title_msg_tips_mgr.social_unread_msgbox_count > 0 then
        social_title_msg_tips_mgr.social_unread_msgbox_count = 0;
        social_title_msg_tips_mgr.update_tips_msg_info({});
    end
end

--[[
 {
        retCode : 0,
        msg : "",
        data : {
            "online_user_count" : 12,
            "social_unread_msgbox_count" : 0
            "social_tips_list" : {
                "online_msg" : "",
                "friend_msg_count" : 0,
                "group_msg_count" : 0,
                "discuss_msg_count" : 0,
                "live_room_msg_count": 0,
                "new_live_room_count": 0,
            },
            "social_pop_list" : {
                delay : 3000,
                list : [
                    {
                        "id" : 1,
                        "title" : "",
                        "url" : ""
                    }
                ]
            }
        }
    }
--]]
function social_title_msg_tips_mgr.update_tips_msg_info(data_in)
    local callback_func = web_register_center.get_func_by_browser("social_update_msg_event", CID_MAINFRAME_QBWEB);
    if callback_func == nil then
        return;
    end

    if social_title_msg_tips_mgr.is_max_social_visible == true then
        social_title_msg_tips_mgr.new_live_room_count = 0;
    else
        local out = comm_center.call_service('i_service_social_need_show_tips_on_web_title', {});
        social_title_msg_tips_mgr.new_live_room_count = out.need;
    end

    if social_title_msg_tips_mgr.is_click_min_mode_social_tab == true then
        social_title_msg_tips_mgr.friend_msg_count = 0;
        social_title_msg_tips_mgr.group_msg_count = 0;
        social_title_msg_tips_mgr.discuss_msg_count = 0;
        social_title_msg_tips_mgr.live_room_msg_count = 0;
        social_title_msg_tips_mgr.online_msg = "";
    elseif social_title_msg_tips_mgr.is_social_visble() == false then
        social_title_msg_tips_mgr.friend_msg_count,
        social_title_msg_tips_mgr.group_msg_count,
        social_title_msg_tips_mgr.discuss_msg_count,
        social_title_msg_tips_mgr.live_room_msg_count = social_title_msg_tips_mgr.get_all_unread_msg_count();
    else
        social_title_msg_tips_mgr.friend_msg_count = 0;
        social_title_msg_tips_mgr.group_msg_count = 0;
        social_title_msg_tips_mgr.discuss_msg_count = 0;
        social_title_msg_tips_mgr.live_room_msg_count = 0;
    end

    local rsp_data_table = web_utility.notify_event_comm_data("social_update_msg_event");
    rsp_data_table["retCode "] = 0;
    rsp_data_table["msg"] = "success";
    rsp_data_table["data"]["online_user_count"] = friend_list_model.get_total_online_count();
    rsp_data_table["data"]["social_unread_msgbox_count"] = social_title_msg_tips_mgr.social_unread_msgbox_count;
    rsp_data_table["data"]["social_tips_list"] = {};
    rsp_data_table["data"]["social_tips_list"]["online_msg"] = social_title_msg_tips_mgr.online_msg;
    rsp_data_table["data"]["social_tips_list"]["friend_msg_count"] = social_title_msg_tips_mgr.friend_msg_count;
    rsp_data_table["data"]["social_tips_list"]["group_msg_count"] = social_title_msg_tips_mgr.group_msg_count;
    rsp_data_table["data"]["social_tips_list"]["discuss_msg_count"] = social_title_msg_tips_mgr.discuss_msg_count;
    rsp_data_table["data"]["social_tips_list"]["live_room_msg_count"] = social_title_msg_tips_mgr.live_room_msg_count;
    rsp_data_table["data"]["social_tips_list"]["new_live_room_count"] = social_title_msg_tips_mgr.new_live_room_count;

    rsp_data_table["data"]["social_pop_list"] = {};
    local tips_list = social_push_tips_mgr.get_ad_list();
    if tips_list ~= nil then
        rsp_data_table["data"]["social_pop_list"]["delay"] = tips_list.delay;
        rsp_data_table["data"]["social_pop_list"]["list"] = tips_list.list;
    end

    local str = base_utility.table_to_json(rsp_data_table);
    web_utility.lua_call_js_ex(callback_func, str, CID_MAINFRAME_QBWEB);

    --上线消息每次发送完后需要清空
    social_title_msg_tips_mgr.online_msg = "";

    return {};
end

function social_title_msg_tips_mgr.on_switch_talk_session(event_id, json_str)
    local param = base_utility.json_to_table(json_str);
    social_title_msg_tips_mgr.current_active_talk_session_id = param.id;

    if social_title_msg_tips_mgr.is_social_visble() == true then
        return
    end
    if param.unread_count > 0 then
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval('social_title_msg_tips_mgr.process_on_switch_talk_session', 100);
    end
end

function social_title_msg_tips_mgr.process_on_switch_talk_session()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('social_title_msg_tips_mgr.process_on_switch_talk_session');
    if social_title_msg_tips_mgr.get_all_unread_msg_count() < 0 then -- 如果已经有未读消息了，那么就不通知UI更新红点，因为on_switch_talk_session场景红点只减不增
        social_title_msg_tips_mgr.update_tips_msg_info();
    end
end

function social_title_msg_tips_mgr.get_all_unread_msg_count()
    local list = comm_center.call_service('i_service_get_contact_msg_list', {});
    local unread_friend_cnt = 0;
    local unread_group_cnt = 0;
    local unread_discussion_cnt = 0;
    local unread_live_room_cnt = 0;
    if list ~= nil then
        for i = 1, #list do
            if list[i].unread_msg_cnt > 0 or list[i].group_type == "MatchLiveRoom" or list[i].group_type == "CommonTopicRoom" then 
                if list[i].type == "friend" then
                    unread_friend_cnt = unread_friend_cnt + 1;
                elseif list[i].type == "group" then
                    if list[i].group_type == "Public" then
                        unread_group_cnt = unread_group_cnt + 1;
                    elseif list[i].group_type == "MobileDiscus" then 
                        unread_discussion_cnt = unread_discussion_cnt + 1;
                    elseif list[i].group_type == "MatchLiveRoom" or list[i].group_type == "CommonTopicRoom" then 
                        if list[i].unread_msg_cnt > 0 then
                            unread_live_room_cnt = unread_live_room_cnt + 1;
                        else
                            --是否有小编直播上墙消息
                            local out = comm_center.call_service('i_service_social_get_unread_live_msg_count', {group_id = list[i].id});
                            unread_live_room_cnt = unread_live_room_cnt + out.count;
                        end
                    end
                end
            end
        end
    end
    return unread_friend_cnt, unread_group_cnt, unread_discussion_cnt, unread_live_room_cnt;
end

function social_title_msg_tips_mgr.on_get_friend_list_done(event_id, json_str)
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval('social_title_msg_tips_mgr.process_get_friend_list_done', 1000);
end

function social_title_msg_tips_mgr.process_get_friend_list_done()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('social_title_msg_tips_mgr.process_get_friend_list_done');
    social_title_msg_tips_mgr.update_tips_msg_info({});
end

--监听好友上线消息
function social_title_msg_tips_mgr.on_recv_friend_report(event_id, json_str)
    if social_title_msg_tips_mgr.is_social_visble() == true then
        return;
    end
    local table_param = base_utility.json_to_table(json_str);
    if table_param.nType == 0xFF  then  
        local extend_msg = base_utility.json_to_table(table_param["szRemark"]);
        if extend_msg.type == "status_change" then
            if extend_msg.gameid == "55555" then
                local cur_status = friend_list_model.get_friend_status(extend_msg.tgp_id);
                if extend_msg.status == "1" then
                    --好友tgp上线
                    --测试发现，可能收到两次上线通知，这里要屏蔽下
                    --避免界面提示两次上线消息
                    if cur_status == friend_list_model.TGP_OFFLINE_STATUS then
                        local friend_info = friend_list_model.get_friend_info(extend_msg.tgp_id);
                        if friend_info ~= nil then
                            social_title_msg_tips_mgr.online_msg = friend_info.name;
                            social_title_msg_tips_mgr.is_click_min_mode_social_tab = false;
                        end
                    else
                        base_utility.log("recv second online report!!", true);
                        return;
                    end
                end

                if cur_status == friend_list_model.TGP_ONLINE_STATUS then
                    if extend_msg.status == "-1" then
                        local friend_info = friend_list_model.get_friend_info(extend_msg.tgp_id);
                        if friend_info ~= nil then
                            social_title_msg_tips_mgr.online_msg = friend_info.name;
                            social_title_msg_tips_mgr.is_click_min_mode_social_tab = false;
                        end
                    end
                end

                local tpfui =
                    base_utility.get_component_adapter("ITpf_ui",
                        "ierd_tgp.tpf_ui.GetTPFUI();");
                tpfui:AddInterval('social_title_msg_tips_mgr.process_friend_status_changed', 10);
            end
        end
    end

end

function social_title_msg_tips_mgr.process_friend_status_changed()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('social_title_msg_tips_mgr.process_friend_status_changed');
    social_title_msg_tips_mgr.update_tips_msg_info({});
end

--监听好友聊天消息
function social_title_msg_tips_mgr.recv_friend_new_msg(event_id, json_str)
    if social_title_msg_tips_mgr.is_social_visble() then
        return;
    end
    
    local msg = base_utility.json_to_table(json_str);
    --查看当前聊天窗口是否已经打开
    if social_title_msg_tips_mgr.current_active_talk_session_id == msg.friend_id then
        return;
    end

    social_title_msg_tips_mgr.is_click_min_mode_social_tab = false;
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval('social_title_msg_tips_mgr.process_on_recv_msg', 1);
end

--监听群组/讨论组/话题房间聊天消息
function social_title_msg_tips_mgr.recv_group_new_msg(event_id, json_str)
    if social_title_msg_tips_mgr.is_social_visble() then
        return;
    end

    local msg = base_utility.json_to_table(json_str);
    --查看当前聊天窗口是否已经打开
    if social_title_msg_tips_mgr.current_active_talk_session_id == msg.group_id then
        return;
    end

    local info = social_network_control.get_my_info();
    if msg.sender == info.id then
        return;
    end

    social_title_msg_tips_mgr.is_click_min_mode_social_tab = false;
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval('social_title_msg_tips_mgr.process_on_recv_msg', 1);
end

function social_title_msg_tips_mgr.process_on_recv_msg()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('social_title_msg_tips_mgr.process_on_recv_msg');
    social_title_msg_tips_mgr.update_tips_msg_info({});
end

--社交面板可见定义相关-----------------------------------------------------------------
function social_title_msg_tips_mgr.click_game_tab_btn(event_id, json_str)
    if social_title_msg_tips_mgr.main_mode == 1 then
        social_title_msg_tips_mgr.is_min_social_visible = false;
    end
end

function social_title_msg_tips_mgr.click_session_tab_btn(event_id, json_str)
    if social_title_msg_tips_mgr.main_mode == 1 then   
        social_title_msg_tips_mgr.is_min_social_visible = true;
        social_title_msg_tips_mgr.is_click_min_mode_social_tab = true;
    end
end

function social_title_msg_tips_mgr.click_contact_tab_btn(event_id, json_str)
    if social_title_msg_tips_mgr.main_mode == 1 then
        social_title_msg_tips_mgr.is_min_social_visible = true;
        social_title_msg_tips_mgr.is_click_min_mode_social_tab = true;
    end
end

function social_title_msg_tips_mgr.open_social_panel(event_id, json_str)
    local param_table = base_utility.json_to_table(json_str);
    if param_table ~= nil and param_table.show ~= nil then
       if 1 == param_table.show then
            social_title_msg_tips_mgr.is_max_social_visible = true;
            social_title_msg_tips_mgr.update_tips_msg_info({});    
       else
           social_title_msg_tips_mgr.is_max_social_visible = false;
       end
    end
end

--判断社交面板打开情况：社交面板打开的情况下，不需要提示
function social_title_msg_tips_mgr.is_social_visble()
    return (social_title_msg_tips_mgr.is_min_social_visible or social_title_msg_tips_mgr.is_max_social_visible);
end

function social_title_msg_tips_mgr.change_mainframe_mode(event_id, json_str)       
    local params = base_utility.json_to_table(json_str);
    if nil ~= params then
        social_title_msg_tips_mgr.main_mode = params.mainframe_mode;
        if params.mainframe_mode == 0 then
            social_title_msg_tips_mgr.is_min_social_visible = false;
            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:AddInterval('social_title_msg_tips_mgr.process_change_mainframe_mode', 1000);
        end
    end
end

function social_title_msg_tips_mgr.process_change_mainframe_mode()   
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('social_title_msg_tips_mgr.process_change_mainframe_mode');
    social_title_msg_tips_mgr.update_tips_msg_info({}); 
end

function social_title_msg_tips_mgr.social_mainwnd_show(param_in)
    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    if data.is_show == 1 then 
        social_title_msg_tips_mgr.is_max_social_visible = true;
        social_title_msg_tips_mgr.update_tips_msg_info({}); 
    else
        social_title_msg_tips_mgr.is_max_social_visible = false;
    end
end

-----------------------------------------------------------------------------------------


-- social_title_msg_tips_mgr.init();
